# coding=utf-8
import json

import pymysql
import random
import time
from datetime import datetime
import threading

from mysql.connector import cursor

# MySQL数据库连接
class changetag:
    def __init__(self):
        # 数据库配置
        config = {
            'host': '192.168.1.100',
            'user': 'tester',
            'password': 'tester1234',
            'database': 'gwza_hardware',
            'charset': 'utf8mb4',
        }
        self.conn = pymysql.connect(**config)
        self.cursor = self.conn.cursor()

    def getmemo(self,flag=None):
        pa = 100162.476562
        if flag == 1:  #变pa
            pa = pa + random.uniform(-1000, 1000)
            # 变值
            pa = str(pa)
        else:
            pa = str(pa)

        # 原始 JSON 字符串
        memo = '{"label": "489617 4684", "pa": "old_value", "elect": "43"}'
        # 将 pa_new 插入到 JSON 字符串中的 "pa" 键
        memo_dict = json.loads(memo)  # 将 JSON 字符串转换为字典
        memo_dict['pa'] = pa  # 更新 "pa" 键的值
        # 变pa
        memo = json.dumps(memo_dict)  # 将更新后的字典转换回 JSON 字符串
        print(memo)

        return memo


    # 定义数据更新函数
    def update_data(self,ids, y,anchors_updated,num,flag):

        #num = 1

        while num > 0:
            for id in ids:
                print(id)
                # 获取当前记录的x, y, z, update_time
                # cursor.execute(f"SELECT x,y,z,update_time FROM map_device WHERE device_id ='{id}'")
                # x, y, z, update_time = cursor.fetchone()
                #x1 = 4000
                x1 = 12022
                # y1 = 20500
                z1 = 14022

                # 随机变动x和y的值（范围是正负40）
                # x_new = x1 + random.uniform(-400, 400)
                # y_new = y + random.uniform(-400, 400)
                x_new = x1
                y_new = y

                # 随机变动z的值（范围是正负1）
                #z_new = z1 + random.uniform(-500, 500)
                z_new = z1
                print(x_new,y_new,z_new)


                # 更新update_time为当前时间
                update_time_new = datetime.now()

                memo_update = self.getmemo(flag)   #获取memo值

                # 更新数据库中的数据
                self.cursor.execute(
                    f"UPDATE map_device SET device_status=1,x={x_new}, y={y_new}, z={z_new}, update_time='{update_time_new}',anchors='{anchors_updated}',memo='{memo_update}' WHERE device_id = '{id}'")
                self.conn.commit()

            # 等待一秒钟
            time.sleep(1)
            print(num)
            num = num - 1

if __name__ == '__main__':

    ct = changetag()
    ids = ['GW4684']
    #y_in = 900
    y_in = 17022
    y_out = 20022
    #yB_out = 23000
    #yB_in = 32000
    anchors1 = '{"total": 1, "time": "2024-09-04 17:00:32", "anchors": [{"id": "GW2846", "dis": 4000},]}'
    anchors2 = '{"total": 1, "time": "2024-09-04 17:00:32", "anchors": [{"id": "GWFFFF", "dis": 4000},]}'
    anchors3 = '{}'

    ct.update_data(ids, y_in, anchors3, 10, 1)
    ct.update_data(ids, y_out, anchors3, 5, 1)
    #ct.update_data(ids, y_out, anchors1, 1, 1)
    #ct.update_data(ids, y_out, anchors1, 2,1)
    #x =19100 x 10200  y 19000  10700
    #ct.update_data(ids, y_in, anchors3, 3, 0)
    #ct.update_data(ids, y_in, anchors3, 10, 0)
    #ct.update_data(ids, y_out, anchors3, 50, 1)

    # ct.update_data(ids, y_in, anchors3, 10, 1)#A区纠正
    # ct.update_data(ids, y_out, anchors3, 10,1)  #A区域 out
    # ct.update_data(ids, yB_out, anchors3, 5, 1) #B区域 out
    # ct.update_data(ids, yB_in, anchors3, 10, 1)  # B区域 纠正
    # ct.update_data(ids, yB_out, anchors3, 10, 1)  # B区域 out
    # ct.update_data(ids, y_out, anchors3, 20, 1) #A






